package com.cryptape.cita_wallet.activity.transfer;

import android.text.TextUtils;
import android.util.Log;
import android.widget.Toast;

import com.cryptape.cita_wallet.activity.RansomActivity;
import com.cryptape.cita_wallet.constant.ConstantUtil;
import com.cryptape.cita_wallet.item.Chain;
import com.cryptape.cita_wallet.item.Currency;
import com.cryptape.cita_wallet.item.Token;
import com.cryptape.cita_wallet.item.Wallet;
import com.cryptape.cita_wallet.item.transaction.AppTransaction;
import com.cryptape.cita_wallet.service.ContractService;
import com.cryptape.cita_wallet.service.http.CITARpcService;
import com.cryptape.cita_wallet.service.http.CytonSubscriber;
import com.cryptape.cita_wallet.service.http.EthRpcService;
import com.cryptape.cita_wallet.service.http.TokenService;
import com.cryptape.cita_wallet.service.http.WalletService;
import com.cryptape.cita_wallet.util.CurrencyUtil;
import com.cryptape.cita_wallet.util.NumberUtil;
import com.cryptape.cita_wallet.util.db.DBWalletUtil;
import com.cryptape.cita_wallet.util.ether.EtherUtil;
import com.cryptape.cita_wallet.util.url.HttpCITAUrls;
import com.mangoworld.android.R;

import org.web3j.abi.datatypes.Address;
import org.web3j.abi.datatypes.Type;
import org.web3j.abi.datatypes.generated.Uint256;
import org.web3j.protocol.core.methods.response.EthSendTransaction;
import org.web3j.utils.Numeric;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.TimeUnit;

import rx.Observable;
import rx.Subscriber;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Func1;
import rx.schedulers.Schedulers;

import static com.cryptape.cita_wallet.activity.transfer.TransferActivity.EXTRA_ADDRESS;
import static com.cryptape.cita_wallet.activity.transfer.TransferActivity.EXTRA_TOKEN;

/**
 * Created by dengming on 19/1/20.
 */

public class RansomTransferPresenter {

    private RansomActivity mActivity;
    private TransferView mTransferView;

    private Token mToken;
    private double mTokenPrice = 0;
    private Wallet mWallet;
    private Currency mCurrency;
    public double num = 0;

    private BigInteger mGasPrice, mGasLimit, mGas;
    private BigInteger mQuota, mQuotaLimit, mQuotaPrice;
    private String mData;
    private double mTokenBalance;
    private double mNativeTokenBalance, mTransferFee;
    public final static String TYPE_LOCK_NATURE = "Nature";
    public final static String TYPE_LOCK_MIX = "Mix";
    public final static String TYPE_LOCK_EMPTY = "Empty";
    public String mLockAddress;

    public RansomTransferPresenter(RansomActivity activity, TransferView transferView) {
        mActivity = activity;
        mTransferView = transferView;

        init();
    }

    public void init() {

        EthRpcService.init(mActivity);
        CITARpcService.init(mActivity, HttpCITAUrls.CITA_NODE_URL);

        initTokenItem();
        getAddressData();
        initWalletData();
        getTokenBalance();
        initTransferFee();
    }

    public void getTokenBalance() {
//        WalletService.getBalanceWithToken(mActivity, mToken).subscribe(new CytonSubscriber<Double>() {
//            @Override
//            public void onNext(Double balance) {
//                mTokenBalance = balance;
////                mTransferView.updateAnyTokenBalance(balance);
//            }
//        });

        WalletService.getBalanceWithNativeToken(mActivity, mToken).subscribe(new Subscriber<Double>() {
            @Override
            public void onNext(Double balance) {
                mNativeTokenBalance = balance;
                mTransferView.updateNativeTokenBalance(balance);
            }

            @Override
            public void onError(Throwable e) {
                e.printStackTrace();
            }

            @Override
            public void onCompleted() {

            }
        });

//        updateLockbalance();

    }

    private void getAddressData() {
        String address = mActivity.getIntent().getStringExtra(EXTRA_ADDRESS);
        mTransferView.updaterReceiveAddress(address);
    }

    private void initTokenItem() {
        mToken = mActivity.getIntent().getParcelableExtra(EXTRA_TOKEN);
//        mTransferView.updateTitleData("闪电兑换");
    }

    private void initWalletData() {
        mWallet = DBWalletUtil.getCurrentWallet(mActivity);
        mTransferView.updateWalletData(mWallet);
    }

    private void initTransferFee() {
        if (EtherUtil.isEther(mToken)) {
            initEthGasInfo();
            mTransferView.initTransferEditValue();
            initTokenPrice();
        } else {
            initCITAQuota();
        }
    }

    private void initEthGasInfo() {
//        mGasLimit = EtherUtil.isNative(mToken) ? ConstantUtil.QUOTA_TOKEN : ConstantUtil.QUOTA_ERC20;
        mGasLimit = ConstantUtil.QUOTA_ERC20_BIG_APPEND;
        mTransferView.startUpdateEthGasPrice();
        EthRpcService.getEthGasPrice().subscribe(new CytonSubscriber<BigInteger>() {
            @Override
            public void onError(Throwable e) {
                e.printStackTrace();
                mTransferView.updateEthGasPriceFail(e);
            }

            @Override
            public void onNext(BigInteger gasPrice) {
                mGasPrice = gasPrice;
                updateGasInfo();
                mTransferView.updateEthGasPriceSuccess(gasPrice);
                updateLockInfo();
            }
        });
    }

    /**
     * get the price of token
     */
    private void initTokenPrice() {
        mCurrency = CurrencyUtil.getCurrencyItem(mActivity);
        TokenService.getCurrency(ConstantUtil.ETH, mCurrency.getName())
                .subscribe(new CytonSubscriber<String>() {
                    @Override
                    public void onNext(String price) {
                        if (TextUtils.isEmpty(price))
                            return;
                        try {
                            mTokenPrice = Double.parseDouble(price);
                            mTransferView.initTransferFeeView();
                        } catch (NumberFormatException e) {
                            e.printStackTrace();
                        }
                    }
                });
    }


    private void initCITAQuota() {
        mQuotaLimit = TextUtils.isEmpty(getTokenItem().getContractAddress()) ? ConstantUtil.QUOTA_TOKEN : ConstantUtil.QUOTA_ERC20;
        CITARpcService.getQuotaPrice(mWallet.address)
                .subscribe(new CytonSubscriber<String>() {
                    @Override
                    public void onError(Throwable e) {
                        e.printStackTrace();
                    }

                    @Override
                    public void onNext(String quotaPrice) {
                        mQuotaPrice = new BigInteger(quotaPrice);
                        initQuotaFee();
                        mTransferView.initTransferFeeView();
                    }
                });

    }

    private void initQuotaFee() {
        mQuota = mQuotaLimit.multiply(mQuotaPrice);
        mTransferFee = NumberUtil.getEthFromWei(mQuota);
        mTransferView.updateCITAQuota(NumberUtil.getDecimalValid_8(mTransferFee) + getFeeTokenUnit());
    }

    public void updateQuotaLimit(BigInteger quotaLimit) {
        mQuotaLimit = quotaLimit;
        initQuotaFee();
    }

    public BigInteger getQuotaLimit() {
        return mQuotaLimit;
    }

    public void initGasLimit(AppTransaction appTransaction) {
//        EthRpcService.getEthGasLimit(appTransaction)
//                .subscribe(new CytonSubscriber<BigInteger>() {
//                    @Override
//                    public void onError(Throwable e) {
//                        e.printStackTrace();
//                    }
//
//                    @Override
//                    public void onNext(BigInteger gasLimit) {
//                        mGasLimit = gasLimit.multiply(ConstantUtil.GAS_LIMIT_PARAMETER);
//                        updateGasInfo();
//                    }
//                });
    }

    public void initGasLimit() {
        org.web3j.protocol.core.methods.request.Transaction transaction = mActivity.getRansomTransactionInfo();
        EthRpcService.getEthGasLimit(transaction)
                .subscribe(new CytonSubscriber<BigInteger>() {
                    @Override
                    public void onError(Throwable e) {
                        e.printStackTrace();
                    }

                    @Override
                    public void onNext(BigInteger gasLimit) {
                        Log.e("MainActivity:","getEthGasLimit:"+gasLimit);
//                        mGasLimit = gasLimit.multiply(ConstantUtil.GAS_LIMIT_PARAMETER);
                        if(gasLimit != null) {
                            mGasLimit = new BigDecimal(gasLimit).multiply(new BigDecimal("1.1")).toBigInteger();
//                        mGasLimit = gasLimit.multiply(new BigInteger("11").divide(new BigInteger("10")));
                            Log.e("MainActivity:", "getEthGasLimit mGasLimit:" + mGasLimit);
                            updateGasInfo();
                        }
                    }
                });
    }

    public void handleActiveAction(String password, String transferValue, String receiveAddress, byte[] data) {
        if (EtherUtil.isEther(mToken)) {
            {
                transferEthErc777(password, transferValue, receiveAddress, data);
            }
        } else {
        }
    }


    public void handleTransferAction(String password, String transferValue, String receiveAddress) {
        if (EtherUtil.isEther(mToken)) {
            if (ConstantUtil.ETH.equals(mToken.getSymbol())) {
                transferEth(password, transferValue, receiveAddress);
            } else {
                transferEthErc20(password, transferValue, receiveAddress);
            }
        } else {
//            if (isNativeToken()) {
//                transferCITAToken(password, transferValue, receiveAddress.toLowerCase());
//            } else {
//                transferCITAErc20(password, transferValue, receiveAddress.toLowerCase());
//            }
        }
    }


    /**
     * transfer origin token of ethereum
     *
     * @param value
     */
    private void transferEth(String password, String value, String receiveAddress) {
        EthRpcService.transferEth(mActivity, receiveAddress, value, mGasPrice, mGasLimit, mData, password)
                .subscribe(new CytonSubscriber<EthSendTransaction>() {
                    @Override
                    public void onError(Throwable e) {
                        mTransferView.transferCITAFail(e);
                    }

                    @Override
                    public void onNext(EthSendTransaction ethSendTransaction) {
                        Log.e("MainActivity", ethSendTransaction.getTransactionHash());
                        if (ethSendTransaction.hasError()) {
                            mTransferView.transferEtherFail(ethSendTransaction.getError().getMessage());
                        } else {
                            mTransferView.transferEtherSuccess(ethSendTransaction);
                        }
                    }
                });
    }


    /**
     * transfer origin token of ethereum
     *
     * @param value
     */
    private void transferEthErc20(String password, String value, String receiveAddress) {
        withdraw_step1(password, value, receiveAddress);
//        EthRpcService.transferErc20(mActivity, mToken, receiveAddress, value, mGasPrice, mGasLimit, password)
//                .subscribe(new CytonSubscriber<EthSendTransaction>() {
//                    @Override
//                    public void onError(Throwable e) {
//                        mTransferView.transferEtherFail(e.getMessage());
//                    }
//
//                    @Override
//                    public void onNext(EthSendTransaction ethSendTransaction) {
//                        if (ethSendTransaction.hasError()) {
//                            mTransferView.transferEtherFail(ethSendTransaction.getError().getMessage());
//                        } else {
//                            mTransferView.transferEtherSuccess(ethSendTransaction);
//                        }
//                    }
//                });
    }


    public void updateLockInfo() {
        mTransferView.startUpdateEthGasPrice();
        Observable.fromCallable(new Callable<String>() {
            @Override
            public String call() throws Exception {
                List<Type> datas = ContractService.lockAddress();
                if (datas != null && datas.size() == 1) {
                    return ((Address) datas.get(0)).getValue();
                }
                return null;
            }
        }).flatMap(new Func1<String, Observable<List<Type>>>() {
            @Override
            public Observable<List<Type>> call(String o) {
                return Observable.fromCallable(new Callable<List<Type>>() {
                    @Override
                    public List<Type> call() throws Exception {
                        if (o == null || o.equals("0x0000000000000000000000000000000000000000")) {
                            return null;
                        }
                        mLockAddress = o;
                        List<Type> datas = ContractService.orderDetail(o);
                        return datas;
                    }
                });
            }
        }).subscribeOn(Schedulers.newThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<List<Type>>() {

                    @Override
                    public void onCompleted() {
                        mTransferView.updateEthGasPriceSuccess(BigInteger.ONE);
                    }

                    @Override
                    public void onError(Throwable e) {
                        mTransferView.updateEthGasPriceSuccess(BigInteger.ONE);
//                        mTransferView.transferEtherFail("获取地址失败");
                    }

                    @Override
                    public void onNext(List<Type> datas) {
                        {
                            if (datas != null && datas.size() > 1) {
                                String type = null;
                                BigInteger lockType = ((Uint256) datas.get(0)).getValue();
                                BigInteger natureType = (BigInteger.ONE);
                                BigInteger mixType = (new BigInteger("2"));
                                BigInteger emptyType = (new BigInteger("3"));
                                if (lockType.compareTo(natureType) == 0) {
                                    type = TYPE_LOCK_NATURE;
                                } else if (mixType.compareTo(lockType) == 0) {
                                    type = TYPE_LOCK_MIX;
                                } else if (emptyType.compareTo(lockType) == 0) {
                                    type = TYPE_LOCK_EMPTY;
                                }
                                BigInteger leftTime = ((Uint256) datas.get(2)).getValue();

                                mActivity.setLockType1(type,leftTime);
                                BigInteger balance = ((Uint256) datas.get(1)).getValue();
                                num = NumberUtil.getEthFromWei(balance)/2d;
                                mTransferView.updateAnyTokenBalance(num);
                                if (mixType.compareTo(lockType) == 0) {
//                                    BigInteger leftTime = ((Uint256) datas.get(2)).getValue();
                                    mActivity.updateLeftTime(leftTime);
                                }
                                if (type.equals(TYPE_LOCK_EMPTY) || type.equals(TYPE_LOCK_NATURE)) {
                                    initGasLimit();
                                }
                            } else {
                                mActivity.setLockType1(null,null);
                                mTransferView.updateAnyTokenBalance(0d);
                                Toast.makeText(mActivity, mActivity.getString(R.string.tip_no_withdraw), Toast.LENGTH_SHORT).show();
                            }
                        }
                    }
                });

    }


    public void updateLockbalance1() {
        mTransferView.startUpdateEthGasPrice();
        Observable.fromCallable(new Callable<String>() {
            @Override
            public String call() throws Exception {
                List<Type> datas = ContractService.lockAddress();
                if (datas != null && datas.size() == 1) {
                    return ((Address) datas.get(0)).getValue();
                }
                return null;
            }
        }).flatMap(new Func1<String, Observable<BigInteger>>() {
            @Override
            public Observable<BigInteger> call(String o) {
                return Observable.fromCallable(new Callable<BigInteger>() {
                    @Override
                    public BigInteger call() throws Exception {

                        List<Type> datas = ContractService.lockbalance(o);
                        if (datas != null && datas.size() > 0) {
                            BigInteger balance = ((Uint256) datas.get(0)).getValue();
                            return balance;
                        }
                        return null;
                    }
                });
            }
        }).subscribeOn(Schedulers.newThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<BigInteger>() {

                    @Override
                    public void onCompleted() {
                        mTransferView.updateEthGasPriceSuccess(BigInteger.ONE);
                    }

                    @Override
                    public void onError(Throwable e) {
                        mTransferView.updateEthGasPriceSuccess(BigInteger.ONE);
//                        mTransferView.transferEtherFail("获取地址失败");
                    }

                    @Override
                    public void onNext(BigInteger o) {
                        if (o != null) {
                            num = NumberUtil.getEthFromWei(o);
                            mTransferView.updateAnyTokenBalance(num);
                        }
                    }
                });
    }


    public void withdraw_step2(String password, String value, String receiveAddress) {
        Observable.fromCallable(new Callable<BigInteger>() {
            @Override
            public BigInteger call() throws Exception {
                List<Type> datas = ContractService.lockbalance(receiveAddress);
                if (datas != null && datas.size() > 0) {
                    BigInteger balance = ((Uint256) datas.get(0)).getValue();
                    return balance;
                }
                return null;
            }
        }).subscribeOn(Schedulers.newThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<BigInteger>() {

                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        mTransferView.transferEtherFail(mActivity.getString(R.string.network_error));
                    }

                    @Override
                    public void onNext(BigInteger o) {
                        if (o == null || o.compareTo(BigInteger.ZERO) < 0) {
                            mTransferView.transferEtherFail("获取余额失败");
//                            Toast.makeText(mActivity,"锁仓余额不足",Toast.LENGTH_SHORT).show();
                            return;
//                            mTransferView.updateAnyTokenBalance(o.doubleValue());
                        }
                        withdraw_step3(password, value, receiveAddress);
                    }
                });

    }

    private void withdraw_step3(String password, String value, String receiveAddress) {

        EthRpcService.withdraw(mActivity, mToken, receiveAddress, value, mGasPrice, mGasLimit, password)
                .subscribe(new CytonSubscriber<EthSendTransaction>() {
                    @Override
                    public void onError(Throwable e) {
                        mTransferView.transferEtherFail(e.getMessage());
                    }

                    @Override
                    public void onNext(EthSendTransaction ethSendTransaction) {
                        if (ethSendTransaction.hasError()) {
                            mTransferView.transferEtherFail(ethSendTransaction.getError().getMessage());
                        } else {
                            mTransferView.transferEtherSuccess(ethSendTransaction);
                        }
                    }
                });
    }

    Subscription subscribe = null;

    public void lockAddress(String password, String value, String receiveAddress, int decimal, String type) {
        Observable.fromCallable(new Callable<String>() {
            @Override
            public String call() throws Exception {
                List<Type> datas = ContractService.lockAddress();
                if (datas != null && datas.size() >= 1) {
                    return ((Address) datas.get(0)).getValue();
                }
                return null;
            }
        }).subscribeOn(Schedulers.newThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<String>() {
                    @Override
                    public void onCompleted() {
//                        mTransferView.transferEtherFail("");
                    }

                    @Override
                    public void onError(Throwable e) {
                        mTransferView.transferEtherFail(e.getMessage());
                    }

                    @Override
                    public void onNext(String s) {
                        if (!TextUtils.isEmpty(s)) {
                            lockbalance(password, value, s, decimal, type, s);
                        } else {
                            mTransferView.transferEtherFail("获取地址失败");
                        }
                    }
                });
    }

    private void withdraw_step1(String password, String value, String receiveAddress) {
        Observable.fromCallable(new Callable<String>() {
            @Override
            public String call() throws Exception {
                List<Type> datas = ContractService.lockAddress();
                if (datas != null && datas.size() >= 1) {
                    return ((Address) datas.get(0)).getValue();
                }
                return null;
            }
        }).subscribeOn(Schedulers.newThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<String>() {
                    @Override
                    public void onCompleted() {
//                        mTransferView.transferEtherFail("");
                    }

                    @Override
                    public void onError(Throwable e) {
                        mTransferView.transferEtherFail(e.getMessage());
                    }

                    @Override
                    public void onNext(String s) {
                        if (!TextUtils.isEmpty(s)) {
                            withdraw_step2(password, value, s);
//                            lockbalance(password, value, s, decimal, type, s);
                        } else {
                            mTransferView.transferEtherFail("获取地址失败");
                        }
                    }
                });
    }


    public void lockbalance(String password, String value, String receiveAddress, int decimal, String type, String contractAddress) {
        subscribe = Observable.interval(5, TimeUnit.SECONDS)
                .take(15)
                .map(new Func1<Long, Boolean>() {
                    @Override
                    public Boolean call(Long aLong) {
                        List<Type> datas = ContractService.lockbalance(contractAddress);
                        if (datas != null && datas.size() >= 1) {
                            BigInteger lockValue = new BigInteger(value).multiply(BigInteger.TEN.pow(decimal));
                            BigInteger balance = ((Uint256) datas.get(0)).getValue();
                            if (balance.subtract(lockValue).compareTo(BigInteger.ZERO) > 0) {
                                return true;

                            }
                        }
                        return false;
                    }
                })
                .subscribeOn(Schedulers.newThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<Boolean>() {

                    @Override
                    public void onCompleted() {
                        mTransferView.transferEtherFail("");
                    }

                    @Override
                    public void onError(Throwable e) {
                        mTransferView.transferEtherFail(e.getMessage());
                    }

                    @Override
                    public void onNext(Boolean result) {
                        if (result) {
                            if (subscribe != null) {
                                subscribe.unsubscribe();
                            }
//                            lockNature(password, value, receiveAddress);
                        }
                    }
                });
    }


    private void transferEntrustEthErc20(String password, String value, String receiveAddress) {
//        lockNature(password,value,receiveAddress);
        withdraw_step1(password, value, receiveAddress);
        EthRpcService.transferEntrustErc20(mActivity, mToken, receiveAddress, value, mGasPrice, mGasLimit, password)
                .subscribe(new CytonSubscriber<EthSendTransaction>() {
                    @Override
                    public void onError(Throwable e) {
                        mTransferView.transferEtherFail(e.getMessage());
                    }

                    @Override
                    public void onNext(EthSendTransaction ethSendTransaction) {
                        if (ethSendTransaction.hasError()) {
                            mTransferView.transferEtherFail(ethSendTransaction.getError().getMessage());
                        } else {
                            lockAddress(password, value, receiveAddress, mToken.decimals, TYPE_LOCK_NATURE);
//                            lockNature(password, value, receiveAddress);
//                            lock();

//                            mTransferView.transferEtherSuccess(ethSendTransaction);
                        }
                    }
                });
    }


    private void transferEthErc777(String password, String value, String receiveAddress, byte[] data) {
        EthRpcService.transferErc777(mActivity, mToken, receiveAddress, value, mGasPrice, mGasLimit, password, data)
                .subscribe(new CytonSubscriber<EthSendTransaction>() {
                    @Override
                    public void onError(Throwable e) {
                        mTransferView.transferEtherFail(e.getMessage());
                    }

                    @Override
                    public void onNext(EthSendTransaction ethSendTransaction) {
                        if (ethSendTransaction.hasError()) {
                            mTransferView.transferEtherFail(ethSendTransaction.getError().getMessage());
                        } else {
                            mTransferView.transferEtherSuccess(ethSendTransaction);
                        }
                    }
                });
    }

    private void updateGasInfo() {
        mGas = mGasPrice.multiply(mGasLimit);
        mTransferFee = NumberUtil.getEthFromWei(mGas);
        mTransferView.initTransferFeeView();
    }

    /**
     * @param gasPrice wei
     */
    public void updateGasPrice(BigInteger gasPrice) {
        mGasPrice = gasPrice;
        updateGasInfo();
    }

    public void updateGasLimit(BigInteger gasLimit) {
        mGasLimit = gasLimit;
        updateGasInfo();
    }

    public void updateData(String data) {
        mData = data;
    }

    public String getData() {
        return mData;
    }

    public Token getTokenItem() {
        return mToken;
    }

    public Wallet getWalletItem() {
        return mWallet;
    }

    public BigInteger getGasLimit() {
        return mGasLimit;
    }

    public boolean isTransferFeeEnough() {
        return mNativeTokenBalance - mTransferFee >= 0;
    }

    /**
     * Check whether transfer value is bigger than balance of wallet
     *
     * @return
     */
//    public boolean checkTransferValueMoreBalance(String transferValue) {
//        if (isNativeToken()) {
//            return Double.parseDouble(transferValue) > (mNativeTokenBalance - mTransferFee);
//        } else {
//            return Double.parseDouble(transferValue) > mTokenBalance;
//        }
//    }

//    public String balanceSubFee() {
//        if (isNativeToken()) {
//            return NumberUtil.getDecimalValid_8(new BigDecimal(mNativeTokenBalance).subtract(new BigDecimal(mTransferFee)).doubleValue());
//        } else {
//            return NumberUtil.getDecimalValid_8(mTokenBalance);
//        }
//    }
    public String getTransferFee() {
        if (mTokenPrice > 0 && EtherUtil.isMainNet()) {
            return NumberUtil.getDecimalValid_8(mTransferFee) + getFeeTokenUnit()
                    + " ≈ " + mCurrency.getSymbol() + " "
                    + NumberUtil.getDecimalValid_2(mTransferFee * mTokenPrice);
        } else {
            return NumberUtil.getDecimalValid_8(mTransferFee) + getFeeTokenUnit();
        }
    }

    public BigInteger getGasPrice() {
        return mGasPrice;
    }

    public boolean isEthERC20() {
        return Numeric.toBigInt(mToken.getChainId()).compareTo(BigInteger.ZERO) < 0
                && !TextUtils.isEmpty(mToken.getContractAddress());
    }

    public boolean isNativeToken() {
        return TextUtils.isEmpty(mToken.getContractAddress());
    }

    public boolean isEther() {
        return Numeric.toBigInt(mToken.getChainId()).compareTo(BigInteger.ZERO) < 0;
    }

    public String getFeeTokenUnit() {
        if (EtherUtil.isEther(mToken)) {
            return " " + ConstantUtil.ETH;
        } else {
            Chain chain = DBWalletUtil.getChainItemFromCurrentWallet(mActivity, mToken.getChainId());
            return chain == null || TextUtils.isEmpty(chain.tokenSymbol) ? "" : " " + chain.tokenSymbol;
        }
    }

}
